<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useTokenStore } from '@/stores/token'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { DocumentCopy, User, SwitchButton, Fold, Expand, House, Menu as MenuIcon, Document, InfoFilled } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const tokenStore = useTokenStore()
const userStore = useUserStore()
const activeIndex = computed(() => route.path)

const isLoggedIn = computed(() => !!tokenStore.getToken())
const displayName = computed(() => userStore.displayName)
const avatarUrl = computed(() => userStore.avatarUrl)

const userInfo = computed(() => userStore.userInfo)

const bmi = computed(() => {
  if (userInfo.value?.height && userInfo.value?.weight) {
    const heightInMeters = userInfo.value.height / 100
    return (userInfo.value.weight / (heightInMeters * heightInMeters)).toFixed(1)
  }
  return '未知'
})

const handleCopy = (text) => {
  navigator.clipboard.writeText(text)
  ElMessage.success('复制成功')
}

const goToUserDetail = () => {
  router.push('/user')
}

const handleSelect = (index) => {
  router.push(index)
}

const handleLogout = () => {
  tokenStore.clearToken()
  userStore.clearUserInfo()
  ElMessage.success('退出登录成功')
  router.push('/')
}

const goToLogin = () => {
  router.push('/login')
}

const goToRegister = () => {
  router.push('/register')
}

const goToFamilyPage = () => {
  router.push('/user/family')
}

const isCollapsed = ref(false)
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}
</script>

<template>
  <div class="common-layout">
    <el-container>
      <el-header height="70px">
        <div class="header-container">
          <div class="logo">
            <img src="@/assets/hui.svg" alt="logo">
            <span v-show="!isCollapsed">膳养智选</span>
          </div>
          <div class="menu-toggle" @click="toggleCollapse">
            <el-icon v-if="isCollapsed"><Expand /></el-icon>
            <el-icon v-else><Fold /></el-icon>
          </div>
          <el-menu
            :default-active="activeIndex"
            class="nav-menu"
            :class="{ 'collapsed': isCollapsed }"
            mode="horizontal"
            @select="handleSelect"
            background-color="#ffffff"
            text-color="#333333"
            active-text-color="#FF8C00">
            <el-menu-item index="/index">
              <el-icon><House /></el-icon>
              <template #title>
                <span v-show="!isCollapsed">首页</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/category">
              <el-icon><MenuIcon /></el-icon>
              <template #title>
                <span v-show="!isCollapsed">食品种类</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/medicalReport">
              <el-icon><Document /></el-icon>
              <template #title>
                <span v-show="!isCollapsed">体检报告</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/about">
              <el-icon><InfoFilled /></el-icon>
              <template #title>
                <span v-show="!isCollapsed">关于我们</span>
              </template>
            </el-menu-item>
          </el-menu>
          <div class="user-actions">
            <template v-if="isLoggedIn">
              <el-dropdown trigger="hover" :hide-timeout="100">
                <div class="user-info">
                  <el-avatar
                    :size="32"
                    :src="avatarUrl"
                    class="user-avatar"
                  />
                  <span class="username">{{ displayName }}</span>
                  <el-icon class="el-icon--right"><arrow-down /></el-icon>
                </div>

                <template #dropdown>
                  <el-dropdown-menu class="custom-dropdown">
                    <div class="dropdown-header">
                      <el-button
                        type="text"
                        class="account-link"
                        @click="goToUserDetail"
                      >
                        <el-icon><user /></el-icon>
                        账号 <el-icon><arrow-right /></el-icon>
                      </el-button>
                    </div>

                    <div class="user-detail">
                      <el-avatar :size="48" :src="avatarUrl" />
                      <div class="user-info-detail">
                        <div class="info-item">
                          <span>昵称：{{ userInfo?.name }}</span>
                          <el-icon
                            class="copy-icon"
                            @click="handleCopy(userInfo?.name)"
                          >
                            <document-copy />
                          </el-icon>
                        </div>
                        <div class="info-item">
                          <span>用户名：{{ userInfo?.username }}</span>
                          <el-icon
                            class="copy-icon"
                            @click="handleCopy(userInfo?.username)"
                          >
                            <document-copy />
                          </el-icon>
                        </div>
                        <div v-if="userInfo?.familyRole" class="family-link-item" @click="goToFamilyPage">
                          <div class="family-link-content">
                            <el-icon class="family-icon"><User /></el-icon>
                            <span>家庭共享</span>
                          </div>
                          <el-icon class="arrow-icon"><arrow-right /></el-icon>
                        </div>
                      </div>
                    </div>

                    <el-divider />

                    <div class="body-stats">
                      <div class="stat-item">
                        <span class="stat-label">身高</span>
                        <span class="stat-value">{{ userInfo?.height || '未知' }}cm</span>
                      </div>
                      <div class="stat-item">
                        <span class="stat-label">体重</span>
                        <span class="stat-value">{{ userInfo?.weight || '未知' }}kg</span>
                      </div>
                      <div class="stat-item">
                        <span class="stat-label">BMI</span>
                        <span class="stat-value">{{ userInfo.bmi? userInfo.bmi : bmi }}</span>
                      </div>
                    </div>

                    <el-dropdown-item divided>
                      <div class="logout-btn" @click="handleLogout">
                        <el-icon><switch-button /></el-icon>
                        退出登录
                      </div>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
            <template v-else>
              <div class="auth-buttons">
                <el-button
                  class="login-btn"
                  @click="goToLogin"
                >
                  <el-icon><user /></el-icon>
                  登录
                </el-button>
                <el-divider direction="vertical" />
                <el-button
                  class="register-btn"
                  @click="goToRegister"
                >
                  注册
                </el-button>
              </div>
            </template>
          </div>
        </div>
      </el-header>

      <el-main>
        <router-view></router-view>
      </el-main>

      <el-footer height="60px">
        <div class="footer-content">
          <p>© 2025 膳养智选 - 您的食品安全管理专家. 保留所有权利.</p>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<style scoped>
.common-layout {
  height: 100vh;
  width: 100vw;
  overflow-x: hidden;
}

.el-container {
  height: 100%;
  width: 100%;
  max-width: 100%;
  margin: 0;
  display: flex;
  flex-direction: column;
}

.el-header {
  padding: 0;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  width: 100%;
  flex-shrink: 0;
  position: relative;
}

.header-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  box-sizing: border-box;
}

.logo {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-right: 20px;
  flex-shrink: 0;
  transition: all 0.3s ease;
}

.logo img {
  height: 45px;
  width: 45px;
  object-fit: cover;
  border-radius: 50%;
}

.logo span {
  font-size: 1.5rem;
  font-weight: bold;
  color: #FF8C00;
  transition: all 0.3s ease;
}

.menu-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  margin: 0 16px;
  color: #666;
}

.menu-toggle:hover {
  background-color: #f5f5f5;
  color: #FF8C00;
}

.menu-toggle .el-icon {
  font-size: 20px;
}

.nav-menu {
  border-bottom: none;
  flex: 1;
  display: flex;
  justify-content: center;
  height: 70px;
  line-height: 70px;
  transition: all 0.3s ease;
}

.nav-menu.collapsed {
  width: auto;
  padding: 0 8px;
}

.nav-menu.collapsed :deep(.el-menu-item) {
  padding: 0 12px;
  width: auto;
}

.el-main {
  background-color: #f8f9fa;
  padding: 20px;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  align-items: flex-start;
  width: 100%;
  box-sizing: border-box;
}

.main-content {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
}

.feature-row {
  width: 100%;
  margin: 0;
}

.feature-card {
  transition: transform 0.3s ease;
  height: calc(100vh - 250px);
  display: flex;
  flex-direction: column;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.card-header {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #FF8C00;
  font-size: 1.2rem;
  font-weight: bold;
  height: 56px;
}

.card-content {
  padding: 30px 20px;
  text-align: center;
  color: #666;
  line-height: 1.8;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.feature-icon {
  font-size: 40px;
  color: #FF8C00;
  margin-bottom: 20px;
}

.el-footer {
  background-color: #ffffff;
  border-top: 1px solid #e6e6e6;
  padding: 0;
  height: 60px !important;
  line-height: 60px;
  flex-shrink: 0;
}

.footer-content {
  text-align: center;
  color: #666;
  font-size: 0.9rem;
  height: 100%;
}

@media screen and (max-width: 1200px) {
  .header-container {
    padding: 0 10px;
  }

  .nav-menu {
    justify-content: flex-start;
  }
}

@media screen and (max-width: 768px) {
  .el-header {
    height: auto !important;
  }

  .header-container {
    flex-direction: column;
    height: auto;
    padding: 10px;
  }

  .nav-menu {
    width: 100%;
    margin: 10px 0;
    justify-content: center;
  }

  .logo {
    margin-right: 0;
    margin-bottom: 10px;
  }

  .user-actions {
    margin-left: 0;
    margin-top: 10px;
  }

  .menu-toggle {
    margin: 0 8px;
  }

  .nav-menu.collapsed :deep(.el-menu-item) {
    padding: 0 8px;
  }
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f5f5;
}

.user-avatar {
  border: 2px solid #FF8C00;
}

.username {
  font-size: 14px;
  color: #333;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.el-divider {
  margin: 0 4px;
  height: 16px;
}

.custom-dropdown {
  width: 280px;
  padding: 0;
  overflow: hidden;
}

.dropdown-header {
  padding: 8px 16px;
  border-bottom: 1px solid #eee;
}

.account-link {
  color: #333;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0;
  height: 24px;
}

.account-link .el-icon {
  font-size: 14px;
}

:deep(.account-link.el-button) {
  padding: 0;
  border: none;
}

.account-link:hover {
  color: #FF8C00;
}

.user-detail {
  padding: 16px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  width: 100%;
  box-sizing: border-box;
}

.user-info-detail {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  width: 100%;
  box-sizing: border-box;
  padding-right: 8px;
}

.copy-icon {
  cursor: pointer;
  color: #909399;
  font-size: 14px;
  flex-shrink: 0;
}

.copy-icon:hover {
  color: #FF8C00;
}

.body-stats {
  padding: 16px;
  display: flex;
  justify-content: space-between;
  width: 100%;
  box-sizing: border-box;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-label {
  font-size: 12px;
  color: #909399;
}

.stat-value {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

:deep(.el-dropdown-menu__item--divided) {
  border-top: 1px solid #eee;
  margin: 0;
  padding: 0;
}

:deep(.el-dropdown-menu__item--divided::before) {
  display: none;
}

.auth-buttons {
  display: flex;
  align-items: center;
  gap: 4px;
}

.login-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #666;
  font-size: 14px;
  padding: 8px 16px;
  border: 1px solid transparent;
  transition: all 0.3s ease;
  background: transparent;
}

.login-btn:hover {
  color: #FF8C00;
  background-color: #fff5e6;
  border-radius: 4px;
  transform: translateY(-1px);
}

.login-btn:active {
  transform: translateY(0);
}

.login-btn .el-icon {
  font-size: 16px;
  transition: all 0.3s ease;
}

.login-btn:hover .el-icon {
  transform: scale(1.1);
}

.el-divider {
  margin: 0 8px;
  height: 20px;
  border-color: #e0e0e0;
}

.register-btn {
  background-color: #FF8C00;
  border-color: #FF8C00;
  color: white;
  padding: 8px 20px;
  font-size: 14px;
  border-radius: 4px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(255, 140, 0, 0.1);
}

.register-btn:hover {
  background-color: #ff9d1f;
  border-color: #ff9d1f;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(255, 140, 0, 0.2);
}

.register-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(255, 140, 0, 0.1);
}

.logout-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #f56c6c;
  padding: 8px 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.logout-btn:hover {
  background-color: #fff1f0;
}

.logout-btn .el-icon {
  font-size: 16px;
}

.family-link-item {
  margin-top: 12px;
  padding: 8px 12px;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: all 0.2s ease;
  color: #409EFF;
}

.family-link-item:hover {
  background: rgba(64, 158, 255, 0.1);
}

.family-link-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.family-icon {
  font-size: 16px;
  color: #409EFF;
}

.family-link-content span {
  font-size: 14px;
  font-weight: 500;
}

.arrow-icon {
  font-size: 14px;
  color: #909399;
  transition: transform 0.2s ease;
}

.family-link-item:hover .arrow-icon {
  transform: translateX(2px);
  color: #409EFF;
}
</style>
